package com.weixin.message;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.weixin.message.common.util.SignUtil;

/**
 * 验证token以及消息处理
 *
 */
@Controller
public class TokenController {

	private static final String TOKEN = "4T5OpiT39p882keA";

	private MessageProcessor processor = new MessageProcessor();

	/**
	 * 处理微信服务器发来的验证请求
	 * 微信接口配置填http://你的域名/weixinService
	 * 
	 * @param validatePojo
	 * @return 验证成功返回随机字符串
	 */
	@RequestMapping(value = "/weixinService", method = RequestMethod.GET
	// 这句一定要加,如果不加默认会返回Content-Type=text/html导致token认证失败
			, produces = "text/plain")
	public @ResponseBody String validate(HttpServletRequest request) {
		return SignUtil.validateSignature(request, TOKEN);
	}

	/**
	 * 处理微信服务器发来的请求
	 * 
	 * @return 返回响应消息
	 */
	@RequestMapping(value = "/weixinService", method = RequestMethod.POST)
	public @ResponseBody void service(HttpServletRequest request, HttpServletResponse response) {
		this.processor.process(request, response);
	}

}
